﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FireStarterModeller.BO;
using FireStarterModeller.UI.Class;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;
using Rhino.Mocks;
using DialogResult=Habanero.Faces.Base.DialogResult;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestShowErrors
    {

        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
            
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin();

        }

        private static IControlFactory GetControlFactory()
        {
            return GlobalUIRegistry.ControlFactory;
        }

        [Test]
        [Ignore("Visual Testing Purposes only")]
        public void Test_Visually()
        {
            //---------------Set up test pack-------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DialogResult dialogResult = (DialogResult) showErrors.ShowDialog();
            //---------------Test Result -----------------------
            MessageBox.Show("DialogResult is " + dialogResult);
        }

        [Test]
        public void Test_Construct_ShouldBeAForm()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(FormWin), showErrors);
        }  

        [Test]
        public void Test_Construct_WhenControlFactoryNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
               new ShowErrors(new DMSolution(), null);
               Assert.Fail("expected ArgumentNullException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("controlFactory", ex.ParamName);
            }
        }        
        
        [Test]
        public void Test_Construct_WhenSolutionNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
               new ShowErrors(null, GetControlFactory());
               Assert.Fail("expected ArgumentNullException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("solution", ex.ParamName);
            }
        }     
        
        [Test]
        public void Test_Construct_ConstructsErrorsViewer()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.IsNotNull(showErrors.ErrorViewerControl);
        }       
        
        [Test]
        public void Test_Construct_ConstructsButtonControlWith3Buttons()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.IsNotNull(showErrors.ButtonGroupControl);
            Assert.AreEqual(3,showErrors.ButtonGroupControl.Controls.Count);
        }      
        
        [Test]
        public void Test_Construct_ConstructsOkButton()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.IsNotNull(showErrors.OkButton);
        }     
        
        [Test]
        public void Test_Construct_ConstructsCancelationButton()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.IsNotNull(showErrors.CancelationButton);
        }

        [Test]
        public void Test_Construct_ContainsErrorViewer()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.Contains(showErrors.ErrorViewerControl, (ICollection) TestUtilsUI.GetAllChildControls(showErrors));
        }

        [Test]
        public void Test_OKButton_WhenClicked_ShouldCloseForm()
        {
            //---------------Set up test pack-------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            showErrors.Show();
            //---------------Assert Precondition----------------
            Assert.IsTrue(showErrors.Visible);
            //---------------Execute Test ----------------------
            showErrors.OkButton.PerformClick();
            //---------------Test Result -----------------------
            Assert.IsFalse(showErrors.Visible);
        }

        [Test]
        public void Test_OKButton_WhenClicked_ShouldHaveOKDialogResult()
        {
            //---------------Set up test pack-------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            showErrors.Show();
            showErrors.DialogResult = DialogResult.Abort;
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(DialogResult.OK, showErrors.DialogResult);
            //---------------Execute Test ----------------------
            showErrors.OkButton.PerformClick();
            //---------------Test Result -----------------------
            Assert.AreEqual(DialogResult.OK, showErrors.DialogResult);
        }

        [Test]
        public void Test_CancelationButton_WhenClicked_ShouldCloseForm()
        {
            //---------------Set up test pack-------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            showErrors.Show();
            //---------------Assert Precondition----------------
            Assert.IsTrue(showErrors.Visible);
            //---------------Execute Test ----------------------
            showErrors.CancelationButton.PerformClick();
            //---------------Test Result -----------------------
            Assert.IsFalse(showErrors.Visible);
        }

        [Test]
        public void Test_CancelationButton_WhenClicked_ShouldHaveCancelDialogResult()
        {
            //---------------Set up test pack-------------------
            ShowErrors showErrors = new ShowErrors(new DMSolution(), new ControlFactoryWin());
            showErrors.Show();
            showErrors.DialogResult = DialogResult.Abort;
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(DialogResult.Cancel, showErrors.DialogResult);
            //---------------Execute Test ----------------------
            showErrors.CancelationButton.PerformClick();
            //---------------Test Result -----------------------
            Assert.AreEqual(DialogResult.Cancel, showErrors.DialogResult);
        }
    }
}
